#include "PSFRunAction.hh"
#include "PSFEventAction.hh"

#include "PSFDetectorConstruction.hh"
#include "PSFRun.hh"
#include "PSFSteppingAction.hh"

#include "G4AnalysisManager.hh"
#include "G4Run.hh"
#include "G4RunManager.hh"
#include "Randomize.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

PSFRunAction::PSFRunAction(PSFEventAction *eventaction)
    : fEventAction(eventaction)
{
  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->SetDefaultFileType("root");
  // If the filename extension is not provided, the default file type (root)
  // will be used for all files specified without extension.
  analysisManager->SetVerboseLevel(1);
  analysisManager->SetNtupleMerging(true);
  // Note: merging ntuples is available only with Root output
  analysisManager->SetFileName("psfhisto");
  // Book histograms, ntuple
  if (fEventAction)
  {
    // Creating ntuple
    analysisManager->CreateNtuple("ntuple", "Time and index of detector");                   // 创建元组
    analysisManager->CreateNtupleDColumn("Energy", fEventAction->GetEnergy());               // 探测器探测光子能量
    analysisManager->CreateNtupleDColumn("Time", fEventAction->GetTime());                   // 探测器探测光子时间
    analysisManager->CreateNtupleIColumn("DetectorIndex", fEventAction->GetDetectorIndex()); // 探测器编号
    analysisManager->CreateNtupleIColumn("NPhoton", fEventAction->GetNPhoton());             // 探测器探测光子数
    analysisManager->CreateNtupleDColumn("PEdep", fEventAction->GetPEdep());                 // 粒子沉积能量
    analysisManager->CreateNtupleIColumn("NPPhoton", fEventAction->GetNPPhoton());           // 产生初始光子数
    analysisManager->CreateNtupleDColumn("PosX", fEventAction->GetPosX());                   // 探测器探测光子位置
    analysisManager->CreateNtupleDColumn("PosY", fEventAction->GetPosY());                   // 探测器探测光子位置
    analysisManager->CreateNtupleDColumn("PosZ", fEventAction->GetPosZ());                   // 探测器探测光子位置
    analysisManager->CreateNtupleDColumn("DirX", fEventAction->GetDirX());                   // 探测器探测光子方向
    analysisManager->CreateNtupleDColumn("DirY", fEventAction->GetDirY());                   // 探测器探测光子方向
    analysisManager->CreateNtupleDColumn("DirZ", fEventAction->GetDirZ());                   // 探测器探测光子方向
    analysisManager->CreateNtupleDColumn("DirTheta", fEventAction->GetDirTheta());           // 探测器探测光子方向
    analysisManager->CreateNtupleDColumn("DirPhi", fEventAction->GetDirPhi());               // 探测器探测光子方向
    analysisManager->FinishNtuple();
  }
  // Set ntuple output file
  // analysisManager->SetNtupleFileName(0, "psfntuple");
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

G4Run *PSFRunAction::GenerateRun()
{

  fRun = new PSFRun();
  return fRun;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void PSFRunAction::BeginOfRunAction(const G4Run *)
{

  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->Reset();
  analysisManager->OpenFile(); // do NOT insert fileName, otherwise it will not accept the name from the .mac file
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void PSFRunAction::EndOfRunAction(const G4Run *)
{

  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->Write();
  analysisManager->CloseFile(false);

  if (isMaster)
  {
    fRun->EndOfRun();
  }
}
